package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.model.Role;
import com.prince.model.User;
import com.prince.page.Page;
import com.prince.web.base.BaseController;
import com.prince.web.service.RoleService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 角色
 * @author Wangpeixi 
 *
 */
@Controller
public class RoleController extends BaseController{

	@Resource
	private RoleService roleService;
	
	
	/**
	 * 条件查询所有角色
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findRoleByName", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findRole(@RequestParam Map<String,Object> params,HttpServletRequest request) {
		User user =(User) request.getSession().getAttribute("user");
		params.put("user", user);
		Page page = getRequestPageRollInfo(params);
		Map<String,Object> map = roleService.findRole(params,page);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	
	
	/**
	 * 添加角色
	 * @param role
	 * @return
	 */
	@AppLog
	@RequestMapping(value="addRole",method=RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> addRole(Role role,@RequestParam Map<String,Object> params,HttpServletRequest request){
		User user =(User) request.getSession().getAttribute("user");
		Map<String,Object> map = roleService.addRole(role,user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 根据id查询角色
	 * @param role
	 * @return
	 */
	@AppLog
	@RequestMapping(value="findRoleById",method=RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findRoleById(Role role){
		Map<String,Object> map = roleService.findRoleById(role.getId());
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 根据角色id查看limitsId
	 * @param role
	 * @return
	 */
	@AppLog
	@RequestMapping(value="findRoleLimitsId",method=RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findRoleLimitsId(Role role){
		Map<String,Object> map = roleService.findRoleLimitsId(role.getId());
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 修改角色
	 * @param role
	 * @return
	 */
	@AppLog
	@RequestMapping(value="updateRole",method=RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> updateRole(Role role){
		Map<String,Object> map = roleService.updateRole(role);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	/**
	 * 删除角色
	 * @param role
	 * @return
	 */
	@AppLog
	@RequestMapping(value="delRole",method=RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> delRole(Role role){
		Map<String,Object> map = roleService.delRole(role);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}

	/**
	 *  父id查询角色
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findUserRoleAll", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findUserRoleAll(@RequestParam Map<String,Object> params,HttpServletRequest request) {
		
		Map<String,Object> map = roleService.findRoleAll();
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 父id查询角色
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findUserRole", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findUserRole(@RequestParam Map<String,Object> params,HttpServletRequest request) {
		User user =(User) request.getSession().getAttribute("user");
		Map<String,Object> map = roleService.findUserRole(user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
}
